﻿#include "MyAppHandlerer.h"
#include "src/shared/renderer/client_app_renderer.h"
#include "../common/Tool.h"
#include "../common/WebSignHelper.h"

namespace client {

	CefRefPtr<CefV8Context> pV8Context = nullptr;
	V8Handler::V8Handler()
	{
		Init();
	}

	void V8Handler::Init()
	{
        //func_map["jk_set_cookies"] = std::bind(&V8Handler::JkSetCookies, this, std::placeholders::_1);
		func_map["jk_get_customerName"] = std::bind(&V8Handler::JkGetCustomerName, this, std::placeholders::_1);
		func_map["jk_qd_close_window"] = std::bind(&V8Handler::JkQdCloseWindow, this, std::placeholders::_1);
		func_map["jk_setMaximizeCallback"] = std::bind(&V8Handler::JkSetMaximizeCallback, this, std::placeholders::_1);
	}
	void V8Handler::SendProcessMessageToBrowser(CefRefPtr<CefProcessMessage> msg)
	{
		CefRefPtr<CefBrowser> pBrowser = CefV8Context::GetCurrentContext()->GetBrowser();
		pBrowser->GetMainFrame()->SendProcessMessage(PID_BROWSER, msg);
	}
	CefRefPtr<CefV8Value> V8Handler::ProcessByUseMap(std::string key, const CefV8ValueList&arguments)
	{
		std::map<std::string, ptr_func>::iterator it = func_map.find(key);
		if (it != func_map.end())
		{
			ptr_func pFun = it->second;
			if (pFun)
			{
				return (pFun)(arguments);
			}
		}
		return CefV8Value::CreateString("");
	}

	bool V8Handler::Execute(const CefString& name,
		CefRefPtr<CefV8Value> object,
		const CefV8ValueList& arguments,
		CefRefPtr<CefV8Value>& retval,
		CefString& exception) {

		retval = ProcessByUseMap(name.ToString(), arguments);

		return true;
	}

	// 获取用户名
	CefRefPtr<CefV8Value> V8Handler::JkGetCustomerName(const CefV8ValueList &arguments)
	{

		//std::wstring path = g_ini.GetCurPathW(L"config.ini");
		std::string  name = g_ini.GetConfigString(L"customerName", L"root", L"");
		return CefV8Value::CreateString(name);
	}

	CefRefPtr<CefV8Value> V8Handler::JkQdCloseWindow(const CefV8ValueList &arguments)
	{
		if (arguments[0]->IsFunction())
		{
			callback_func_jkqdclosewindowcallback_ = arguments[0];
			callback_context_jkqdclosewindowcallback_ = CefV8Context::GetCurrentContext();
		}
		return CefV8Value::CreateString("");
	}

	CefRefPtr<CefV8Value> V8Handler::JkSetMaximizeCallback(const CefV8ValueList&arguments)
	{
		if (arguments.size() > 0 && arguments[0]->IsFunction())
		{
			// 确认下回调函数的参数位置
			callback_func_jksetmaximizecallback_ = arguments[0];
			callback_context_jksetmaximizecallback_ = CefV8Context::GetCurrentContext();
		}

		CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("jk_setMaximizeCallback");
		SendProcessMessageToBrowser(msg);

		return CefV8Value::CreateString("");
	}



 //   CefRefPtr<CefV8Value> V8Handler::JkSetCookies(const CefV8ValueList& arguments)
	//{
 //       if (arguments.size() >= 1)
 //       {
 //           auto jsonString = arguments.at(0)->GetStringValue();

 //           CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("jk_set_cookies");
 //           msg->GetArgumentList()->SetString(0, jsonString);

 //           SendProcessMessageToBrowser(msg);
 //           return CefV8Value::CreateString("1");
 //       }
 //       return CefV8Value::CreateString("0");

	//}

}
